//
//  UserData.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-6-3.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "UserData.h"
#import "ShipVo.h"

@implementation UserData

@synthesize level           = _level; 
@synthesize exp             = _exp; 
@synthesize money           = _money; 
@synthesize gold            = _gold;
@synthesize userName        = _userName;
@synthesize shipArray       = _shipArray;
@synthesize fightShipArray  = _fightShipArray;
@synthesize date            = _date;

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) 
    {
        //数据初始化
        self.level  = 1;
        self.exp  = 0;
        self.money  = 0;
        self.gold   = 0;
        self.userName = @"大头船长";
        //获取系统当前时间
        NSDate * date = [NSDate date];
        NSTimeInterval sec = [date timeIntervalSinceNow];
        NSDate * currentDate = [[NSDate alloc] initWithTimeIntervalSinceNow:sec];
        self.date = currentDate;
        [currentDate release];
        
        //给默认船
        self.shipArray = [CCArray array];
        ShipVo* vo = [ShipVo shipVoWithId:112001];
        [vo.attackSystemIds setObject:[NSNumber numberWithInt:111001] forKey:@"1"];
        [self.shipArray addObject:vo];
        //默认出战的船
        self.fightShipArray = [CCArray array];
        ShipVo* vof = [ShipVo shipVoWithId:112001];
        [vof.attackSystemIds setObject:[NSNumber numberWithInt:111001] forKey:@"1"];
        [self.fightShipArray addObject:vof];
    }
 
    return self;
}

- (id) initWithCoder: (NSCoder *)coder  
{  
    if (self = [super init])  
    {  
        self.level      = [coder decodeIntForKey:@"level"]; 
        self.exp        = [coder decodeIntForKey:@"exp"]; 
        self.money      = [coder decodeIntForKey:@"money"]; 
        self.gold       = [coder decodeIntForKey:@"gold"]; 
        self.userName   = [coder decodeObjectForKey:@"userName"]; 
        self.date       = [coder decodeObjectForKey:@"date"]; 
        self.shipArray  = (CCArray *)[NSKeyedUnarchiver unarchiveObjectWithData:[coder decodeObjectForKey:@"shipArray"]];
        self.fightShipArray  = (CCArray *)[NSKeyedUnarchiver unarchiveObjectWithData:[coder decodeObjectForKey:@"fightShipArray"]];
    }  
    return self;  
}  

- (void) encodeWithCoder: (NSCoder *)coder  
{  
    [coder encodeInt:self.level forKey:@"level"]; 
    [coder encodeInt:self.exp forKey:@"exp"]; 
    [coder encodeInt:self.money forKey:@"money"]; 
    [coder encodeInt:self.gold forKey:@"gold"]; 
    [coder encodeObject:self.userName forKey:@"userName"]; 
    [coder encodeObject:self.date forKey:@"date"]; 
    [coder encodeObject:[NSKeyedArchiver archivedDataWithRootObject:self.shipArray] forKey:@"shipArray"]; 
    [coder encodeObject:[NSKeyedArchiver archivedDataWithRootObject:self.fightShipArray] forKey:@"fightShipArray"]; 
}  

- (void)dealloc
{  
    if(_userName != nil)
    {
        [_userName release];
        _userName = nil;
    }
    
    if(_shipArray != nil)
    {
        [_shipArray removeAllObjects];
        [_shipArray release];
        _shipArray = nil;
    }
    
    if(_fightShipArray != nil)
    {
        [_fightShipArray removeAllObjects];
        [_fightShipArray release];
        _fightShipArray = nil;
    }
    
    if(_date != nil)
    {
        [_date release];
        _date = nil;
    }
    
    [super dealloc];  
} 

@end
